package com.liang.controller;

import com.liang.common.lang.FunctionUtil;
import com.liang.common.lang.Result;
import com.liang.entity.*;
import com.liang.service.*;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @name: CAdminController
 * @author: 李昂
 * @date: 2020-12-24 16:46o
 * @description：CAdminController 管理员操作的controller
 * @update: 2020-12-24 16:46
 */

@RestController
@RequestMapping("/admin")
@AllArgsConstructor
public class CAdminController {

    private CUserService userService;
    private CFileService fileService;
    private CNoticeService noticeService;
    private CWeijiService weijiService;
    private CKaoshiService kaoshiService;
    private CActiveService activeService;
    private CNewsService newsService;

    //修改用户状态
    @PutMapping("/statue")
    public Result ChangeStatue(@RequestParam("statue") String statue, @RequestParam("id") String id) {
        if (userService.ChangeStatue(id, statue)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }


    //-----------------------------------------社团基本属性-------------------------------------------


    //上传社团活动图片
    @PutMapping("/active")
    public Result activePicture(@RequestBody CFile file) {
        if (fileService.addFileMsg(file)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }


    //删除社团活动图片
    @DeleteMapping("/active/{id}")
    public Result deletePicture(@PathVariable("id") Integer id) {
        if (fileService.deleteFileMsg(id)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }

    //查看所有活动图片
//    @GetMapping("/active/{type}")
//    public Result getActivePictures(@PathVariable("type") String type) {
//        return Result.succ(fileService.getFileMsgs(type));
//    }


    //---------------------------------社团成员crud----------------------------------

    //初始化密码
    @PreAuthorize("hasAnyRole('admin')")
    @PutMapping("/pwd/{id}")
    public Result ChushiPwd(@PathVariable("id") String id) {
        if (userService.ChushiPwd(id)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }

    //删除注册的用户  未审核通过的人  已审核通过的人员只能冻结不能进行删除
    @PreAuthorize("hasAnyRole('admin')")
    @DeleteMapping("/register/{id}")
    public Result deleterRegist(@PathVariable("id") String id) {

        if (userService.deleterRegist(id)) {
            return Result.succ();
        } else {
            return Result.fail();
        }

    }


    //管理员批量添加用户，只有管理员才有此权限，因此必须在此方法上加入@PreAuthorize("hasAnyRole('ADMIN')")
    @PreAuthorize("hasAnyRole('admin')")
    @PostMapping("/registers")
    public Result regists(@RequestBody List<CUser> users) {
        //遍历users  给定初始状态
        users.forEach(item -> {
            item.setStatus("1");
            item.setJoinTime(FunctionUtil.formatDate(new Date()));
            item.setPassword(new BCryptPasswordEncoder().encode("123456"));
            item.setRole("normal");
            item.setId(FunctionUtil.uuid());
        });

        if (userService.registers(users)) {
            return Result.succ();
        } else {
            return Result.fail();
        }

    }


    //修改用户权限
    @PreAuthorize("hasAnyRole('admin')")
    @PutMapping("/role")
    public Result ChangeRole(@RequestParam("id") String id, @RequestParam("role") String role) {
        if (userService.ChangeRole(id, role)) {
            return Result.succ();
        } else {
            return Result.fail();
        }

    }


    // ------------------社团通知-----------------------

    //添加社团公告
    @PostMapping("/notice")
    public Result Notice(@RequestBody CNotice notice) {

        notice.setTime(FunctionUtil.formatDate(new Date()));
        if (noticeService.addNotice(notice)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }

    //获取社团公告
    @GetMapping("/notice")
    public Result getNotice(@RequestParam(value = "id", required = false) String id) {
        return Result.succ(noticeService.getNotice(id));
    }

    //删除社团公告
    @DeleteMapping("/notice/{id}")
    public Result delNotice(@PathVariable("id") int id) {
        if (noticeService.delNotice(id)) {
            return Result.succ();
        } else {
            return Result.fail();
        }

    }


    //违纪人通告
    @GetMapping("/weiji")
    public Result getWeiji(@RequestParam(value = "name") String name) {
        return Result.succ((userService.getWeiji(name)));
    }

    //添加违纪通告
    @PostMapping("/weiji")
    public Result insertWeiji(@RequestBody CWeiji weiji) {
        if (weijiService.insertWeiji(weiji)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }

    //删除违纪通告
    @DeleteMapping("/weiji/{id}")
    public Result deleteWeiji(@PathVariable("id") int id) {
        if (weijiService.deleteWeiji(id)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }

    //查看违纪记录
    @GetMapping("/weijirecord")
    public Result lookweiji(@RequestParam(value = "sno", required = false) String sno) {
        return Result.succ(weijiService.lookweiji(sno));
    }


    //------------------成绩录入------------------


    //插入考试主题 以及考试人员成绩
    @PostMapping("/score")
    public Result addKaoshiRecord(@RequestParam("title") String title, @RequestBody List<CKaoshi> kaoshis) {

        String timer = FunctionUtil.formatDate(new Date());
        //通过循环遍历 给title和clazz赋值
        kaoshis.forEach(item -> {
            item.setTitle(title);
            item.setClazz(title.substring(0, 2));
            item.setTime(timer);
        });

        if (kaoshiService.insertScores(kaoshis)) {
            return Result.succ();
        } else {
            return Result.fail();
        }

    }

    //获取考试成绩
    @GetMapping("/score")
    public Result showKaoshiType(@RequestParam(value = "title", required = false) String title) {
        return Result.succ(kaoshiService.showScore(title));
    }

    //获取考试成绩 平均分  最高分  最低分
    @GetMapping("/GroupScore")
    public Result GroupKaoshi() {
        return Result.succ(kaoshiService.GroupKaoshi());
    }

    //删除某一次的考试成绩
    @DeleteMapping("/score")
    public Result deleteKaoshi(@RequestParam(value = "title") String title) {
        if (kaoshiService.deleteKaoshi(title)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }

    //-----------------------职务分配-------------------------


    //-------------------------活动消息--------------------------


    //添加活动信息
    @PostMapping("/active")
    public Result publicActive(@RequestBody CActive active) {
        if (activeService.insertAcive(active)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }

    //获取报名人员
    @GetMapping("/active")
    public Result getUsers(@RequestParam("id") int id) {
        return Result.succ(activeService.getUsers(id));
    }

    //删除创建的活动
    @DeleteMapping("/active")
    public Result deleteActive(@RequestParam("id") int id) {
        if (activeService.deleteActive(id)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }


    //-------------------------社团新闻---------------------

    //发布新闻消息
    @PostMapping("/news")
    public Result publicNews(@RequestBody CNews news) {

        news.setTime(FunctionUtil.formatDate(new Date()));
        news.setLooks(0);
        if (newsService.publicNews(news)) {
            return Result.succ();
        } else {
            return Result.fail();
        }

    }


    //获取新闻列表
    @GetMapping("/news")
    public Result getNewsList() {
        return Result.succ(newsService.getNewsList());
    }

    //删除新闻
    @DeleteMapping("/news/{id}")
    public Result deleteNews(@PathVariable("id") int id) {

        if (newsService.deleteNews(id)) {
            return Result.succ();
        } else {
            return Result.fail();
        }

    }

}
